﻿using System.Threading.Tasks;
using AutoMapper;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using K9Admin.Remoting;
using K9Nano.Application.MultiTenancy;
using Microsoft.AspNetCore.Authorization;

namespace K9Nano.Admin.MultiTenancy
{
    [Authorize]
    public class TenantServiceImpl : TenantService.TenantServiceBase
    {
        protected readonly ITenantAppService TenantAppService;
        protected readonly IMapper Mapper;

        public TenantServiceImpl(ITenantAppService tenantAppService, IMapper mapper)
        {
            TenantAppService = tenantAppService;
            Mapper = mapper;
        }

        public override async Task<UUID> Create(TenantCreateRequest request, ServerCallContext context)
        {
            var input = Mapper.Map<TenantCreateInput>(request);
            var tenant = await TenantAppService.CreateAsync(input, context.CancellationToken);
            return new UUID { Id = tenant.Id.ToString() };
        }

        public override async Task<Empty> Delete(UUID request, ServerCallContext context)
        {
            await TenantAppService.DeleteAsync(request.Id, context.CancellationToken);
            return new Empty();
        }
    }
}